//按钮点击事件
import { ajax, toast, print, cardCache,output} from 'nc-lightapp-front';
import linkBudgetApi from '../../../../../public/components/linkbudget/linkbudget';
import linkVoucherApi from '../../../../../public/components/linkvoucher/linkvoucher';
import linkQueryApi from '../../../../../public/components/linkquery/linkquerybills';
import config from '../config';
import _ from 'lodash-es';
import api from '../../../common/api';

export default function (props, key) {
    const checkedRows = props.table.getCheckedRows(config.bodycode);
    const record = _.get(checkedRows, '[0].data.values', {});
    const billId = _.get(record, 'pk_jkbx.value', '');
    const tradeType = _.get(record, 'djlxbm.value', '');
    const billType = _.get(record, 'pk_billtype.value', '');
    if(checkedRows.length === 0){
        toast({
            color: 'warning',
            content: this.state.multiLang['select_one_row']
        });
        return;
    }
    if (key === 'print') {
        let funcode = '';
        api.getBillLinkInfo({
            openbillid: billId,
            tradetype: tradeType
        }).then((res) => {
            console.log(res);
            funcode = res.appcode;
            const billdata=[];
            checkedRows.forEach(item=>{
                var obj={}
                const record = _.get(item, 'data.values', {});
                obj.billId=_.get(record, 'pk_jkbx.value','');
                obj.billType= _.get(record, 'djlxbm.value', '');
                billdata.push(obj)
            })
            ajax({
                url:'/nccloud/erm/pub/ErmBatchPrintValidaAction.do',
                data:{
                    billdata:billdata
                },
                success:(res)=>{
                    console.log(res);
                    if(!_.get(res, 'data.canPrint', false)){
                        toast({
                            color: 'danger',
                            content: res.data.errMesg
                        });
                        return;
                    }else{             
                        ajax({
                            url:'/nccloud/erm/pub/ErmBatchPrintTemplatesAction.do',
                            data:{
                                billType:billdata[0].billType
                            },
                        success:(res)=>{
                            if(res.data){
                                const record = _.get(checkedRows, '[0].data.values', {});
                                const tradeType = _.get(record, 'djlxbm.value', '');
                                const oidsData=[]
                                billdata.forEach(item=>{
                                    oidsData.push(item.billId)
                                })
                                let nodekey = '';
                                if (tradeType.startsWith('2647')) {
                                    nodekey = '2647_IWEB';
                                    // funcode = '201102HKSP';
                                } else if (tradeType.startsWith('263')) {
                                    nodekey = '2631_IWEB';
                                    // funcode = '201102JKSP';
                                } else if (tradeType.startsWith('264a')) {
                                    nodekey = '264a_WEB';
                                    // funcode = '201102TZSP';
                                } else if (tradeType.startsWith('261')) {
                                    nodekey = '2611_IWEB';
                                    // funcode = '201102SQSP';
                                } else if (tradeType.startsWith('262')) {
                                    nodekey = '2621_IWEB';
                                    // funcode = '201102YTSP';
                                } else if (tradeType.startsWith('264')) {
                                    nodekey = '2641_IWEB';
                                    // funcode = '201102BXSP';
                                }
                                print(
                                    'pdf',
                                    '/nccloud/erm/pub/ErmPrintBillAction.do', //后台服务url
                                    {
                                        funcode, //功能节点编码，即模板编码
                                        nodekey, //模板节点标识
                                        billtype: billdata[0].billType, //单据类型
                                        oids:oidsData // 功能节点的数据主键   oids含有多个元素(['1001A41000000000A9LR','1001A410000000009JDD'])时为批量打印,
                                    }
                                    )
                                } else {
                                    toast({
                                        color: 'error',
                                        content: res.data.errMesg
                                    });
                                }
                            }
                        })
                    }
                }
            })
        })   
    } else if (key === 'export') {
        let funCode = config.appcode;
        let appcode = config.appcode;
        let nodekey = '';
        if (tradeType.startsWith('2647')) {
            nodekey = '2647_list';
            // funcode = '201102HKSP';
        } else if (tradeType.startsWith('263')) {
            nodekey = '263X_list';
            // funcode = '201102JKSP';
        } else if (tradeType.startsWith('264a')) {
            nodekey = '264a_list';
            // funcode = '201102TZSP';
        } else if (tradeType.startsWith('261')) {
            nodekey = '261X_list';
            // funcode = '201102SQSP';
        } else if (tradeType.startsWith('262')) {
            nodekey = '262X_list';
            // funcode = '201102YTSP';
        } else if (tradeType.startsWith('264')) {
            nodekey = '264X_list';
            // funcode = '201102BXSP';
        }
        const billData=[];
        checkedRows.forEach(item=>{
            var obj={}
            const record = _.get(item, 'data.values', {});
            obj.billId=_.get(record, 'pk_jkbx.value','');
            obj.billType= _.get(record, 'djlxbm.value', '');
            billData.push(obj)
        })
        const oidsData=[];
        checkedRows.forEach(item=>{
            const record = _.get(item, 'data.values', {});
            oidsData.push(_.get(record, 'pk_jkbx.value',''))
        })
        console.log(oidsData);
        ajax({
            url:'/nccloud/erm/pub/ErmBatchPrintValidaAction.do',
            data:{
                billdata:billData
            },
            success:(res) =>{
                if(!_.get(res, 'data.canPrint', false)){
                    toast({
                        color: 'danger',
                        content: '仅支持同种交易类型单据的批量输出，请重新勾选！'
                    });
                    return;
                }else{
                    ajax({
                        url:'/nccloud/platform/print/queryTemplates.do',
                        data:{
                            funcode:funCode,
                            nodekey:nodekey,
                            appcode:appcode,
                            oids:oidsData
                        },
                        success:(res)=>{
                            if(res.data){
                                let printTemplateID=res.data[0].m_ctemplateid
                                let printTemplateName=res.data[0].m_vtemplatename
                                let params={
                                    billtype:billType,
                                    funcode:funCode,
                                    nodekey:nodekey,
                                    appcode:appcode,
                                    oids:oidsData,
                                    userjson:billType,
                                    outputType:'output',
                                    outputMode:'excel',
                                    MultiConfig:'1',
                                    pdfConfig:false,
                                    printTemplateID:printTemplateID,
                                    htmlConfig:false,
                                    prefix:'',
                                    threshold:0,
                                    dsPositionPolicy:4,
                                    fileVolume:16000,
                                    sheetNamePreFix:'新的工作表',
                                    isDifDocDifFile:false,
                                    canModifyType:false,
                                    printTemplateName:printTemplateName
                                }
                                let paramsNew = {
                                    ...params,
                                    output: true
                                }
                                console.log(paramsNew);
                                output(
                                    {    
                                        url:'/nccloud/erm/billmanage/BillOutPutAction.do',
                                        data:paramsNew
                                    }
                                )
                            }else{
                                toast({
                                    color: 'error',
                                    content: res.data.errMesg
                                });
                            }
                        }
                    })
                }
            }
        })
    } else {
        if (checkedRows.length !== 1) {
            toast({
                color: 'warning',
                content: this.state.multiLang['select_one_row']
            });
            return;
        }else{
            switch (key) {
                // 联查申请单
                case 'link_sqd':
                    if (tradeType.startsWith('264a')) {
                    } else {
                        ajax({
                            url: '/nccloud/erm/loanmanage/LinkFysqAction.do',
                            data: {
                                tradetype: tradeType, //交易类型
                                openBillId: billId //单据主键ok
                            },
                            success: (result) => {
                                if (result.data && result.data.jkdBillModel) {
                                    this.setState({
                                        isLinkQueryFysqModelShowed: true,
                                        linkQueryFysqModelSourceData: {
                                            linkData: result,
                                            tradetype: tradeType,
                                            openBillId: this.state.billId
                                        }
                                    });
                                } else if (result.data && result.data.sqdList) {
                                    result.data.sqdList.forEach((item) => {
                                        linkQueryApi.link({
                                            data: {
                                                openbillid: item.pk,
                                                tradetype: item.tradetype,
                                                props: this.props
                                            }
                                        });
                                    });
                                } else if (result.data && result.data.tradetype && result.data.pk) {
                                    linkQueryApi.link({
                                        data: {
                                            openbillid: result.data.pk,
                                            tradetype: result.data.tradetype,
                                            props: this.props
                                        }
                                    });
                                } else {
                                    toast({
                                        color: 'warning',
                                        content: this.state.multiLang['query_link_none']
                                    }); /* 国际化处理： 未联查到相关数据!*/
                                }
                            }
                        });
                    }
                    break;
        
                // 联查预提单
                case 'link_ytd':
                    if (tradeType.startsWith('264a')) {
                    } else {
                        ajax({
                            url: '/nccloud/erm/expenseaccount/expenseaccountLinkQueryAction.do',
                            data: {
                                queryType: 'fyyt',
                                tradetype: tradeType, //交易类型
                                openBillId: billId //单据主键ok
                            },
                            success: (result) => {
                                if (result.data) {
                                    this.setState({
                                        isLinkQueryFyytModelShowed: true,
                                        linkQueryFyytModelSourceData: {
                                            linkData: result,
                                            openBillId: billId,
                                            tradetype: tradeType
                                        }
                                    });
                                } else {
                                    toast({
                                        color: 'warning',
                                        content: this.state.multiLang['query_link_none']
                                    }); /* 国际化处理： 未联查到相关数据!*/
                                }
                            }
                        });
                    }
                    break;
                // 联查借款单
                case 'link_jkd':
                    // 借款单
                    if (tradeType.startsWith('264a')) {
                    } else if (tradeType.startsWith('264')) {
                        ajax({
                            url: '/nccloud/erm/expenseaccount/expenseaccountLinkQueryAction.do',
                            data: {
                                queryType: 'jkbx',
                                tradetype: tradeType, //交易类型
                                openBillId: billId //单据主键ok
                            },
                            success: (result) => {
                                if (result.data) {
                                    this.setState({
                                        isLinkQueryJkbxModelShowed: true,
                                        linkQueryJkbxModelSourceData: {
                                            linkData: result,
                                            tradetype: tradeType,
                                            openBillId: billId
                                        }
                                    });
                                } else {
                                    toast({
                                        color: 'warning',
                                        content: this.state.multiLang['query_link_none']
                                    }); /* 国际化处理： 未联查到相关数据!*/
                                }
                            }
                        });
                    } else {
                        ajax({
                            url: '/nccloud/erm/erbudgetmg/FysqLinkQueryAction.do',
                            data: {
                                userjson: JSON.stringify({ openbillid: billId, pk_tradetype: tradeType, tradetype: 'jk' })
                            },
                            success: (result) => {
                                if (result.data) {
                                    this.setState({
                                        isLinkQueryJkbxModelShowed: true,
                                        linkQueryJkbxModelSourceData: {
                                            linkData: result,
                                            tradetype: tradeType,
                                            openBillId: billId
                                        }
                                    });
                                } else {
                                    toast({
                                        color: 'warning',
                                        content: this.state.multiLang['query_link_none']
                                    }); /* 国际化处理： 未联查到相关数据!*/
                                }
                            }
                        });
                    }
                    break;
                // 联查报销单
                case 'link_bxd':
                    // 借款单
                    if (tradeType.startsWith('264a')) {
                    } else if (tradeType.startsWith('263')) {
                        ajax({
                            url: '/nccloud/erm/expenseaccount/expenseaccountLinkQueryAction.do',
                            data: {
                                queryType: 'jkbx',
                                tradetype: tradeType, //交易类型
                                openBillId: billId //单据主键ok
                            },
                            success: (result) => {
                                if (result.data) {
                                    this.setState({
                                        isLinkQueryJkbxModelShowed: true,
                                        linkQueryJkbxModelSourceData: {
                                            linkData: result,
                                            tradetype: tradeType,
                                            openBillId: billId
                                        }
                                    });
                                } else {
                                    toast({
                                        color: 'warning',
                                        content: this.state.multiLang['query_link_none']
                                    }); /* 国际化处理： 未联查到相关数据!*/
                                }
                            }
                        });
                    } else {
                        ajax({
                            url: '/nccloud/erm/erbudgetmg/FysqLinkQueryAction.do',
                            data: {
                                userjson: JSON.stringify({ openbillid: billId, pk_tradetype: tradeType, tradetype: 'bx' })
                            },
                            success: (result) => {
                                if (result.data) {
                                    this.setState({
                                        isLinkQueryJkbxModelShowed: true,
                                        linkQueryJkbxModelSourceData: {
                                            linkData: result,
                                            tradetype: tradeType,
                                            openBillId: billId
                                        }
                                    });
                                } else {
                                    toast({
                                        color: 'warning',
                                        content: this.state.multiLang['query_link_none']
                                    }); /* 国际化处理： 未联查到相关数据!*/
                                }
                            }
                        });
                    }
                    break;
        
                // 联查审批
                case 'link_sp':
                    this.setState({
                        isApproveDetailShowed: true,
                        approveDetailSourceData: {
                            billid: billId,
                            billtype: tradeType
                        }
                    });
                    break;
        
                // 联查往来单据
                case 'link_wldj':
                    // TODO 缺接口没有billdata数据
                    // let billdata = props.createExtCardData(props.meta.getMeta().code, 'head', bodyarr);
                    ajax({
                        url: '/nccloud/erm/erm/billmanage/BillLinkJsAction.do',
                        data: {
                            openbillid: billId,
                            pk_billtype: billType
                        },
                        success: (data) => {
                            console.log(data);
                            props.specialOpenTo(data.data.ys.url, {
                                id: data.data.ys.id,
                                pagecode: data.data.ys.pagecode,
                                appcode: data.data.ys.appcode,
                                scene: data.data.ys.scene,
                                status: data.data.ys.status,
                                scene: 'lc'
                            });
                            props.specialOpenTo(data.data.yf.url, {
                                id: data.data.yf.id,
                                pagecode: data.data.yf.pagecode,
                                appcode: data.data.yf.appcode,
                                scene: data.data.yf.scene,
                                status: data.data.yf.status
                            });
                        },
                        error: (data) => {
                            toast({ content: data.message, color: 'danger' });
                        }
                    });
                    break;
        
                // 联查预算
                case 'link_ys':
                    linkBudgetApi.link({
                        data: {
                            tradetype: tradeType,
                            openbillid: billId
                        },
                        success: (data) => {
                            this.setState({
                                isInspectionShowed: true,
                                inspectionSourceData: data
                            });
                        }
                    });
                    break;
        
                // 联查凭证
                case 'link_pz':
                    linkVoucherApi.link({
                        data: {
                            props: props,
                            record: {
                                relationID: billId,
                                pk_billtype: billType
                            },
                            appid: props.getSearchParam('ar')
                        }
                    });
                    break;
                default:
                    break;
            }
        }
    }
}
